package cn.edu.tsinghua.gui;
/*
 * @(#)XYPlot.java	1.6 99/06/19 00:18:00
 *
 * Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */


import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.media.jai.*;
import javax.swing.*;

/**
 * A class to plot histograms (primarily)
 * Single band per object (bar chart)
 *
 * @author Dennis Sigel
 */

public class XYPlot extends JComponent {

    private int[] data;
    private int[][] multidata; // nc.
    private int max;
    int[] multimax; // nc.

   /**
    * Default constructor
    */
    public XYPlot() {
        super();
    }

    private void setData(int[] array) {
        data = new int[array.length];
        max = -1;

        for ( int i = 0; i < array.length; i++ ) {
            data[i] = array[i];

            if ( data[i] > max ) {
                max = data[i];
            }
        }
    }

    public void plot(int[] array) {
    	multidata = null;
        setData(array);
        repaint();
    }

    /**
     * multispectral
     * @param array
     */
    public void plot(int[][] array) {
    	setData(array);
    	repaint();
    }
    
    /**
     * 
     * @param array
     */
    private void setData(int[][] array) {
        multidata = new int[array.length][array[0].length];
        multimax = new int[array.length];

        for ( int i = 0; i < array[0].length; i++ ) {
        	for (int b=0; b<array.length; b++) {
	            multidata[b][i] = array[b][i];
	
	            if ( multidata[b][i] > multimax[b] ) {
	                multimax[b] = multidata[b][i];
	            }
        	}
        }
    }
    
    /**
     * Plotter
     */
    public synchronized void paintComponent(Graphics g) {
    	if (multidata == null) {
    		Graphics2D g2D = null;
            if (g instanceof Graphics2D) {
                g2D = (Graphics2D)g;
            } else {
                return;
            }

            if ( data == null ) return;

            int width = getSize().width;
            int height = getSize().height;

            g2D.setColor(getBackground());
            g2D.fillRect(0, 0, width, height);
            g2D.setColor(Color.white);

            int length = data.length;
            float slope_x = (float) width / (float) length;
            float slope_y = (float) height / (float) max;

            for ( int i = 0; i < length; i++ ) {
               int x = (int) ((float)i*slope_x);
               int y = (int) ((float)data[i]*slope_y);
               g.drawLine(x, height, x, height - y);
            }
    	}
    	else {
    		Graphics2D g2D = null;
            if (g instanceof Graphics2D) {
                g2D = (Graphics2D)g;
            } else {
                return;
            }

            if ( data == null && multidata == null) { return; }

            int width = getSize().width;
            int height = getSize().height;

            g2D.setColor(getBackground());
            g2D.fillRect(0, 0, width, height);
            
            int length = multidata[0].length;
            float slope_x = (float) width / (float) length;
            
            for (int b=0; b<3; b++) {
            	switch (b) {
            	case 0: g2D.setColor(Color.blue); break;
            	case 1: g2D.setColor(Color.green); break;
            	case 2: g2D.setColor(Color.red); break;
            	}
            	float slope_y = (float) height / (float) multimax[b];
            	for ( int i = 0; i < length; i++ ) {
                    int x = (int) ((float) i*slope_x)+b;
                    int y = (int) ((float)multidata[b][i]*slope_y);
                    g.drawLine(x, height, x, height - y);
                 }
            }
            
    	}
        
    }
}
